import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;

import util.ApplicationProperties;

public class ReadFromFile {
    private static File root = new File("C:/Users/Marco/Documents/Serientitel");

    public static List<String> read(String series, String language, int season) {
        convert(series, language, season);

        List<String> result = new ArrayList<String>(24);
        // String charset = "UTF-8";
        String charset = Charset.defaultCharset().name();
        // Charset.defaultCharset().name(); // kann z. B. auf
        // UTF-8 umgestellt
        // werden
        // String charset = "UTF-8";

        try {
            Scanner sc = new Scanner(new File(root, series + "/" + language
                    + "/Staffel " + season), charset).useDelimiter("\n");
            while (sc.hasNextLine()) {
                result.add(sc.nextLine().trim());
            }

            for (String s : result) {
                System.out.println("\"" + s + "\", ");
            }
            return result;
        }
        catch (FileNotFoundException e) {
            System.err.println("Das angegeben File zur Serie " + series
                    + " Staffel " + season + " wurde nicht gefunden");
            return null;
        }
    }

    public static boolean convert(String series, String language, int season) {
        try {
            File textfile = new File(root, series + "/" + language
                    + "/Staffel " + season);
            String charset = ApplicationProperties.getCharset();
            int position = 3; // Spaltennummer, Standardspaltennummer

            boolean isAlreadyConverted = false;
            boolean hasAlreadyAskedForColumn = false;
            String newline = System.getProperty("line.separator");
            List<String> result = new ArrayList<String>(24);

            boolean firstLine = true;
            Scanner sc = new Scanner(textfile, charset).useDelimiter("\n");
            while (sc.hasNext()) {
                String lineContent = sc.next();
                if (lineContent.isEmpty()) {
                    continue;
                }
                String[] lineparts = lineContent.split("\t");
                if (lineparts.length == 1) {
                    if (firstLine) {
                        isAlreadyConverted = true;
                        break;
                    }
                    else {
                        firstLine = false;
                        continue;
                    }
                }

                firstLine = false;
                if (!hasAlreadyAskedForColumn) {
                    Integer[] possibleValues = new Integer[lineparts.length];
                    for (int i = 1; i <= lineparts.length; i++) {
                        possibleValues[i - 1] = Integer.valueOf(i);
                    }
                    position = Integer
                            .parseInt(JOptionPane
                                    .showInputDialog(
                                            null,
                                            "In welcher Spaltennummer befindet sich der Serientitel?",
                                            "Spaltennummer",
                                            JOptionPane.QUESTION_MESSAGE, null,
                                            possibleValues,
                                            possibleValues[position - 1])
                                    .toString());
                    hasAlreadyAskedForColumn = true;
                }

                String episodeName = lineparts[position - 1].trim();
                // Fussnoten entfernen
                if (episodeName.endsWith("]")) {
                    if (episodeName.charAt(episodeName.length() - 5) == '[') {
                        episodeName = episodeName.substring(0,
                                episodeName.length() - 5);
                    }
                    else if (episodeName.charAt(episodeName.length() - 4) == '[') {
                        episodeName = episodeName.substring(0,
                                episodeName.length() - 4);
                    }
                    else if (episodeName.charAt(episodeName.length() - 3) == '[') {
                        episodeName = episodeName.substring(0,
                                episodeName.length() - 3);
                    }
                }
                if (episodeName.startsWith("\"")) {
                    episodeName = episodeName.substring(1);
                }
                if (episodeName.endsWith("\"")) {
                    episodeName = episodeName.substring(0,
                            episodeName.length() - 1);
                }
                result.add(episodeName);
            }
            if (!isAlreadyConverted) {
                Writer output = new BufferedWriter(new FileWriter(textfile));
                for (String s : result) {
                    output.write(s + newline);
                }
                output.flush();
                output.close();
                System.out.println("converted");
                return true;
            }
            System.out.println("not converted");
        }
        catch (IOException e) {
            System.err.println("Fehler beim Konvertieren");
            e.printStackTrace();
        }
        return false;
    }
}
